; (function () {
    //return
    /**
   * Definition for a binary tree node.
   * function TreeNode(val, left, right) {
   *     this.val = (val===undefined ? 0 : val)
   *     this.left = (left===undefined ? null : left)
   *     this.right = (right===undefined ? null : right)
   * }
   */
    /**
     * @param {TreeNode} root
     * @return {number[][]}
     */
    let left = {
        val: 2,
        left: null
    }
    let root = {
        val: 1,
        left: left,
        right: null,
    }

    var levelOrder = function (root) {
        if (!root) return []
        let res = []
        let queue = [root]
        let arr = []
        let list = []
        while (queue.length) {
            let current = queue.shift()
            current.left && arr.push(current.left)
            current.right && arr.push(current.right)

            list.push(current.val)
            if (queue.length === 0) {
                res.push(list)
                queue = arr
                list = []
                arr = []
            }
        }

        return res
    };
    console.log(levelOrder(root));

})();


;(function(){
  //return
  var levelOrder = function(root) {
    //二叉树的层序遍历
    let res = [], queue = [];
    queue.push(root);
    if(root === null) {
        return res;
    }
    while(queue.length !== 0) {
        // 记录当前层级节点数
        let length = queue.length;
        //存放每一层的节点
        let curLevel = [];
        for(let i = 0;i < length; i++) {
            let node = queue.shift();
            curLevel.push(node.val);
            // 存放当前层下一层的节点
            node.left && queue.push(node.left);
            node.right && queue.push(node.right);
        }
        //把每一层的结果放到结果数组
        res.push(curLevel);
    }
    return res;
};
})();